Jupyter で Mathematica を使う¶

2024/12/27 滝内

Jupyter を経由して Mathematicaが無料で利用できるとの情報を知った。職場での一括ライセンス更新がなくなってしまったが,再び Mathematicaが利用できるとうれしいので早速導入してみる。

Wolfram Engineを利用するらしい

https://www.wolfram.com/engine/index.php.ja

Wolfram Engineをダウンロードから Windows 10+ を選択。

Wolfram IDがない場合は登録する。私は以前のIDを利用。

ダウンロードしたファイルを実行する。

インストールした後にWolfram Engineを起動して,WindowsにWolfram Engineを設定する。

https://support.wolfram.com/ja/46069

The Wolfram Engine requires one-time activation on this computer.

Visit https://wolfram.com/engine/free-license to get your free license.

Wolfram ID: ***
Password:

Wolfram Engine activated. See https://www.wolfram.com/wolframscript/ for more information.
Wolfram Language 14.1.0 Engine for Microsoft Windows (64-bit)
Copyright 1988-2024 Wolfram Research, Inc.

anacondaを利用しているので,anacondaで gitを install

> conda search git
> conda install git
> conda list

https://github.com/WolframResearch/WolframLanguageForJupyterより gitを使ってファイルを取得。

git clone https://github.com/WolframResearch/WolframLanguageForJupyter.git

WolframLanguageForJupyter フォルダができるのでそこに移動してスクリプトを実行。

> cd .\WolframLanguageForJupyter\
> ./configure-jupyter.wls add

これでJupyter Lab を立ち上げてNew>Notebook kernelをWolfram Language 14.1などにすると Mathematicaのコマンドが実行できる。

Mathematica engine for Jupyter 検索結果からの先人のお知恵やChatGPTのアドバイスを拝借。

Manipulate とAnimate など動的インターフェイスの利用をしたかったが, Manipulateなどでのパラメータを自分で操作することはできないみたい。計算結果の出力は指定しないとImage 形式となるみたい。

In [2]:
Plot[ Sin[x], {x, 0, 2 Pi}]
Out[2]:
Output
In [ ]:
StreamPlot[{-1 - x^2 + y, 1 + x - y^2}, {x, -3, 3}, {y, -3, 3}]
In [ ]:
ContourPlot[Cos[x] + Cos[y], {x, 0, 4 Pi}, {y, 0, 4 Pi}]
In [4]:
Manipulate[
    v =1;
    Plot[Sin[2 Pi (x / lambda - v t / lambda)], {x, 0, 2 π}, PlotRange -> {{0, 2 π}, {-1.1, 1.1}}, 
    AxesLabel -> {"x", "Amplitude"}],
    {t, 0, 2}, {lambda, 1, 3}
]
Out[4]:
Output
In [ ]:
Animate[
    func := Sin[Sqrt[x^2 + y^2]- t]/Sqrt[x^2 + y^2];
    Plot3D[func, {x, -6*Pi, 6*Pi}, {y, -6*Pi, 6*Pi},
        BoxRatios -> {1, 1, 0.2}, PlotRange -> {-1, 1}],
    {t, 0, Pi} 
]
In [7]:
Integrate[Cos[x], x]
Out[7]:
Output
In [8]:
%7 // TeXForm
Out[8]:
$$\sin (x)$$
In [9]:
%7 // InputForm
Out[9]:
Sin[x]
In [10]:
Solve[x^2 - 2 == 0, x]
Out[10]:
Output
In [11]:
%10 // TeXForm
Out[11]:
$$\left\{\left\{x\to -\sqrt{2}\right\},\left\{x\to \sqrt{2}\right\}\right\}$$
In [8]:
Clear[TeXEq]

SetAttributes[TeXEq, HoldFirst];
TeXEq[expr_, f_: FullSimplify] := 
  TeXForm[HoldForm[expr] == f[ReleaseHold[expr]]]
In [11]:
D[Sin[x]^2, x] // TeXEq
Out[11]:
$$\frac{\partial \sin ^2(x)}{\partial x}=\sin (2 x)$$
In [12]:
Integrate[Sin[x], x] // TeXEq
Out[12]:
$$\int \sin (x) \, dx=-\cos (x)$$
In [13]:
Integrate[Sin[x]^2, {x, 0, Pi}] // TeXEq
Out[13]:
$$\int_0^{\pi } \sin ^2(x) \, dx=\frac{\pi }{2}$$